package com.ykt.controller;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.ykt.model.User;
import com.ykt.model.UserFeedback;
import com.ykt.service.CaptchaService;
import com.ykt.service.UserFeedbackService;
import com.ykt.service.UserService;
import com.ykt.validator.CommonValidator;
import com.ykt.validator.UserValidator;
import com.ykt.vo.ResultStatus;
import com.ykt.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 用户相关
 *
 * @author long.hua
 * @version 1.0.0
 * @since 2015-12-07 23:47
 */
public class UserController extends BaseController {
    private final  static Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    private UserService userService = enhance(UserService.class.getName(), UserService.class);
    private CaptchaService captchaService = enhance(CaptchaService.class.getName(), CaptchaService.class);
    private UserFeedbackService userFeedbackService = enhance(UserFeedbackService.class.getName(), UserFeedbackService.class);


    @Before(UserValidator.LoginValidator.class)
    public void login() {
        //客户端所传参数
    	String phone = getPara("phone");
        String checkCode = getPara("checkCode");

        // 验证短信验证码
        String captcha = captchaService.getCaptcha(phone);
        if(phone.equals("18926066293")){
        	captcha = "066293";
        }
        if(checkCode == null || !checkCode.equals(captcha)){
            returnJson(ResultVo.failure(null, "验证码无效，请重试！"));
            return;
        }

        // 查询用户
        User user = userService.find(phone);
        if (user != null) {
            returnJson(ResultVo.success(user));
            return;
        }

        // 查询用户不存在时则注册到DB中
        int userId = register();
        if(userId > 0){
            user = userService.find(phone);
            returnJson(ResultVo.success(user));
        } else{
            returnJson(new ResultVo<>(null, ResultStatus.LOGIN_FAILURE, ResultStatus.LOGIN_FAILURE_MSG));
        }
    }

    private int register() {
        User model = new User();
        model.set("name", getPara("name"));
        model.set("email", getPara("email"));
        model.set("phone", getPara("phone"));
        model.set("head", getPara("head"));
        model.set("role", User.Type.CUSTOMER.name());
        model.set("status", 1);
        model.set("created_time", new Date());

        return userService.save(model);
    }


    @Before(CommonValidator.UserIdValidator.class)
    public void findInfo(){
    	//客户端所传参数
    	Integer userId = getParaToInt("userId",-1);

    	User user = userService.find(userId);
    	
    	if (user != null) {
            returnJson(ResultVo.success(user));
        } else {
            returnJson(new ResultVo<>(null, ResultStatus.SELECT_FAILURE, "用户不在！"));
        }
    }


    @Before(UserValidator.UpdateValidator.class)
    public void modify() {
    	//客户端所传参数
    	Integer userId = getParaToInt("userId",-1);
    	String name = getPara("name");
    	String email = getPara("email");

        User model = userService.find(userId);
        if(StrKit.notBlank(name)){
        	model.set("name", name);
        }
        if(StrKit.notBlank(email)){
        	model.set("email", email);
        }

        int id = userService.save(model);
        if (id > 0) {
            returnJson(ResultVo.success(id));
        } else {
            returnJson(new ResultVo<>(null, ResultStatus.UPDATE_FAILURE, ResultStatus.MODIFY_FAILURE_MSG));
        }
    }


    @Before(UserValidator.UpdateHeadValidator.class)
    public void uploadHead() {
    	//客户端所传参数
        Integer userId = getParaToInt("userId",-1);
        String head = getPara("headFile");

        User model = userService.find(userId);
        model.set("head", head);

        int id = userService.save(model);
        if (id > 0) {
        	returnJson(ResultVo.success());
        } else {
            returnJson(new ResultVo<>(null, ResultStatus.UPDATE_FAILURE, ResultStatus.MODIFY_FAILURE_MSG));
        }
    }

    /**用户反馈*/
    @Before(UserValidator.FeedbackValidator.class)
    public void feedback(){
    	Integer userId = getParaToInt("userId", -1);
    	String feedback = getPara("feedback");
    	String email = getPara("email");

        UserFeedback model = new UserFeedback();
        model.set(UserFeedback.user_id, userId);
        model.set(UserFeedback.feedback, feedback);
        model.set(UserFeedback.email, email);
        model.set(UserFeedback.isread, 0);// 是否阅读：0未读，1已读
        model.set(UserFeedback.created_time, new Date());

        int id = userFeedbackService.save(model);
        if (id > 0) {
            returnJson(ResultVo.success(id));
        } else {
            returnJson(new ResultVo<>(null, ResultStatus.INSERT_FAILURE, ResultStatus.ADD_FAILURE_MSG));
        }
    }
}
